<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
use think\Request;
use think\View;
Class Gongyi extends Index
{
	public function __construct(Request $request)
	{
		parent::__construct($request);
	}
	/**
	* 公益列表
	*/
	public function gylist(){
		$page = Db::name('gongyi')->paginate(6,false,['type' => 'bootstrap','var_page' => 'page']);
		$gongyi = $page->all();
		foreach ($gongyi as $key => $value) {
			$gongyi[$key]['nickname'] = Db::name('user')->where(['userid'=>$gongyi[$key]['userid']])->value('nickname');
		}
		$this->assign('page',$page);
		$this->assign('gongyi',$gongyi);
		$this->assign('title','公益活动');
		return view('gylist');
	}
	/**
	* 公益详情
	*/
	public function gydetail(){
		$gydetail = Db::name('gongyi')->where(['gyid'=>input('gyid')])->find();
		$gydetail['nickname'] = Db::name('user')->where(['userid'=>$gydetail['userid']])->value('nickname');
		$gycontent = Db::name('gycomment')->where(['gyid'=>$gydetail['gyid']])->select();
		// dump($gydetail);die;
		foreach ($gycontent as $key => $value) {
			$gycontent[$key]['nickname'] = Db::name('user')->where(['userid'=>$gycontent[$key]['userid']])->value('nickname');
			$gycontent[$key]['photo'] = Db::name('user')->where(['userid'=>$gycontent[$key]['userid']])->value('photo');
			if($gycontent[$key]['huifuid']!=0){
				$gycontent[$key]['huifunickname'] = Db::name('user')->where(['userid'=>$gycontent[$key]['huifuuserid']])->value('nickname');
			}else{
				$gycontent[$key]['huifunickname']='';
			}
		}
		// dump($gycontent);die;
		if(empty($gycontent)){
			$gycontent='';
		}
		$this->assign('title',$gydetail['gytitle']);
		$this->assign('gycontent',$gycontent);
		$this->assign('gongyi',$gydetail);
		// dump($gycontent);die;
		return view('gydetail');
	}
	/**
	* 公益评论
	*/
	public function gycomment(){
		$data['gyid']=input('post.gyid');
		$data['gycontent']=htmlspecialchars(input('post.gycontent'));
		$data['userid']=session('userid');
		$data['gytime']=time();
		$data['huifuid']=input('post.huifuid');
		$data['huifuuserid']=input('post.huifuuserid');
		$gycontent = Db::name('gycomment')->insert($data);
		if($gycontent){
			jsBack('评论成功','/api/gongyi/gydetail?gyid='.input('post.gyid'));
		}else{
			jsBack('评论失败');
		}
	}



	/**
	* 捐助列表
	*/
	public function helplist(){
		$page = Db::name('help')->paginate(6,false,['type' => 'bootstrap','var_page' => 'page']);
		$help = $page->all();
		foreach ($help as $key => $value) {
			$help[$key]['nickname'] = Db::name('user')->where(['userid'=>$help[$key]['userid']])->value('nickname');
		}
		$this->assign('page',$page);
		$this->assign('help',$help);
		$this->assign('title','社会捐助');
		return view('helplist');
	}
	/**
	* 捐助详情
	*/
	public function helpdetail(){
		$helpdetail = Db::name('help')->where(['helpid'=>input('helpid')])->find();
		$helpdetail['nickname'] = Db::name('user')->where(['userid'=>$helpdetail['userid']])->value('nickname');
		$helpmark = Db::name('helpmark')->where(['helpid'=>$helpdetail['helpid']])->select();
		// dump($helpdetail);die;
		foreach ($helpmark as $key => $value) {
			$helpmark[$key]['nickname'] = Db::name('user')->where(['userid'=>$helpmark[$key]['userid']])->value('nickname');
		}
		// dump($helpmark);die;
		if(empty($helpmark)){
			$helpmark='';
		}
		$this->assign('title',$helpdetail['helptitle']);
		$this->assign('helpmark',$helpmark);
		$this->assign('help',$helpdetail);
		// dump($helpmark);die;
		return view('helpdetail');
	}
	/**
	* 捐助寄语
	*/
	public function helpmark(){
		$data['helpid']=input('post.helpid');
		$data['helpcontent']=htmlspecialchars(input('post.helpcontent'));
		$data['userid']=session('userid');
		$data['helptime']=time();
		$helpcontent = Db::name('helpmark')->insert($data);
		if($helpcontent){
			jsBack('评论成功','/api/gongyi/helpdetail?helpid='.input('post.helpid'));
		}else{
			jsBack('评论失败');
		}
	}
}